//
//  ONEChatLoginViewController.m
//  ONEChat
//
//  Created by 楼楚 on 15/12/16.
//  Copyright © 2015年 Chu Lou. All rights reserved.
//

#import "ONEChatLoginViewController.h"
#import "AppDelegate.h"

@interface ONEChatLoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userText;
@property (weak, nonatomic) IBOutlet UITextField *pwdText;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation ONEChatLoginViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
}
- (IBAction)loginClick {
    [self.view endEditing:YES];

    
    ONEChatUserInfo *userInfo = [ONEChatUserInfo sharedONEChatUserInfo];
    userInfo.user = self.userText.text;
 
    userInfo.pwd =self.pwdText.text;
   

    [self login];
}
- (void)login
{
    [self.view endEditing:YES];
    __weak typeof(self) selfVc = self;
    
    
    [[ONEChatXMPPTool sharedONEChatXMPPTool] xmppUserLogin:^(XMPPResultType type) {
        [selfVc handleResultType:type];
    }];
}

/*
 处理登录结果
 */
- (void)handleResultType:(XMPPResultType)type
{
    //主线程刷新UI
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view];
        switch (type) {
            case XMPPResultTypeLoginSuccess:
                NSLog(@"登录成功");
                [self enterMainPage];
                break;
            case XMPPResultTypeLoginFailue:
                NSLog(@"登录失败");
                [MBProgressHUD showError:@"用户名或密码不正确"toView:self.view ];
                break;
            case XMPPResultTypeNetErr:
                [MBProgressHUD showError:@"网络炸啦" toView:self.view ];
            default:
                break;
        }
    });
}
- (void) enterMainPage{
    //    更改用户的登录状态为yes
    [ONEChatUserInfo sharedONEChatUserInfo].loginStatus = YES;
        //把用户登录成功的数据，保存到沙盒
    [[ONEChatUserInfo sharedONEChatUserInfo]saveUserInfoToSanbox];
    
    //隐藏模态窗口
    [self dismissViewControllerAnimated:NO completion:nil];
    //登录成功来到主界面
    //登录成功后来到主界面
    //    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    //    self.view.window.rootViewController = storyboard.instantiateInitialViewController;
    [UIStoryboard showInitialVCWithName:@"Main"];
}


@end
